package com.ecoolex.chinadutyfree.retailer.core.manager;

import com.ecoolex.chinadutyfree.retailer.core.dao.DirectMailGoodsOrderReceiverDAO;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrder;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrderReceiver;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrderReceiverExample;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.user.system.entity.UserReceiver;
import com.ecoolex.user.system.ws.UserAddressService;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.Date;

@Service
public class DirectMailGoodsOrderReceiverManager {

    @Autowired
    private DirectMailGoodsOrderReceiverDAO dao;

    @Reference
    private UserAddressService userAddressService;

    public DirectMailGoodsOrderReceiver builder(Integer receiverId, Integer userId) {
        if (Check.isNull(receiverId)) {
            return null;
        }
        UserReceiver userReceiver = userAddressService.getUserReceiverByIdAndUserId(receiverId, userId);
        BizAssert.isTrue(Check.notNull(userReceiver), ResultCode.VERIFY.build(20), "收件人地址不存在");

        DirectMailGoodsOrderReceiver receiver = new DirectMailGoodsOrderReceiver();
        receiver.setReceiver(userReceiver.getReceiver());
        receiver.setPhone(userReceiver.getPhone());
        receiver.setRegionId(userReceiver.getRegionId());
        receiver.setAddress(userReceiver.getAddress());

        return receiver;
    }

    public DirectMailGoodsOrderReceiver add(DirectMailGoodsOrder order, DirectMailGoodsOrderReceiver receiver, String createUser) {
        receiver.setOrderId(order.getOrderId());
        receiver.setDeleted(DirectMailGoodsOrderReceiver.NOT_DELETED);
        receiver.setCreateUser(createUser);
        receiver.setCreateTime(Date.from(Instant.now()));
        receiver.setUpdateUser(createUser);
        receiver.setUpdateTime(Date.from(Instant.now()));
        dao.insert(receiver);
        return receiver;
    }

    public DirectMailGoodsOrderReceiver getByOrderId(Integer orderId) {
        return dao.selectByPrimaryKeyWithLogicalDelete(orderId, DirectMailGoodsOrderReceiver.NOT_DELETED);
    }
    
    public DirectMailGoodsOrderReceiver getPossibleBeDeletedByOrderId(Integer orderId) {
        return dao.selectByPrimaryKey(orderId);
    }

    public void deleteByOrderId(DirectMailGoodsOrder order, String updateUser) {
        DirectMailGoodsOrderReceiver record = new DirectMailGoodsOrderReceiver();
        record.setDeleted(DirectMailGoodsOrderReceiver.IS_DELETED);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        DirectMailGoodsOrderReceiverExample example = new DirectMailGoodsOrderReceiverExample();
        example.createCriteria().andOrderIdEqualTo(order.getOrderId()).andDeletedEqualTo(DirectMailGoodsOrderReceiver.NOT_DELETED);

        dao.updateByExampleSelective(record, example, DirectMailGoodsOrderReceiver.Column.deleted, DirectMailGoodsOrderReceiver.Column.updateUser, DirectMailGoodsOrderReceiver.Column.updateTime);
    }

    public DirectMailGoodsOrderReceiver updateReceiver(DirectMailGoodsOrderReceiver receiver,String updateUser) {
        receiver.setUpdateUser(updateUser);
        receiver.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(receiver, DirectMailGoodsOrderReceiver.Column.excludes(DirectMailGoodsOrderReceiver.Column.deleted,DirectMailGoodsOrderReceiver.Column.createTime,DirectMailGoodsOrderReceiver.Column.createUser));
        return receiver;
    }
}